<template>
  <div>
    <vxe-toolbar>
      <template #buttons>
        <el-button type="success" icon="el-icon-plus" size="medium" @click="insertEvent">新增记录</el-button>
        <!-- <el-button type="success" icon="el-icon-printer" size="medium" @click="printEvent">打印</el-button> -->
        <vxe-input style="float: right;" v-model.trim.lazy="filterName" type="search" placeholder="快捷检索"
          @input="searchEvent"></vxe-input>
      </template>
    </vxe-toolbar>
    <template v-for="(column,index) in columns">
      <vxe-checkbox style="margin-bottom: 10px;" v-model="column.visible" :key="index" @change="$refs.XTable.refreshColumn()">{{ column.title }}</vxe-checkbox>
    </template>
    <vxe-table resizable ref="XTable" show-overflow border max-height="800" stripe highlight-hover-row highlight-current-row
      :loading="loading" :data="dataList" @sort-change="sortChange">
      <vxe-table-column field="coacheNames" title="教练员" sortable></vxe-table-column>
      <vxe-table-column field="matchDate" title="比赛日期" sortable></vxe-table-column>
      <vxe-table-column field="matchName" title="赛事名称" sortable></vxe-table-column>
      <vxe-table-column field="matchEvent" title="参赛项目" sortable></vxe-table-column>
      <vxe-table-column field="matchEventItem" title="参赛小项" sortable></vxe-table-column>
      <vxe-table-column field="delegation" title="代表队" sortable></vxe-table-column>
      <vxe-table-column field="matchRank" title="名次" sortable></vxe-table-column>
      <vxe-table-column field="recordBreak" title="破纪录" sortable></vxe-table-column>
      <vxe-table-column field="multiPlayerMatch" title="个人/团体" sortable></vxe-table-column>
  <!--    <vxe-table-column field="regPR" title="注册省/市" sortable></vxe-table-column>
      <vxe-table-column field="regCT" title="注册市/区" sortable></vxe-table-column>
      <vxe-table-column field="reg" title="注册县" sortable></vxe-table-column> -->
      <vxe-table-column field="memo" title="备注" sortable></vxe-table-column>
      <vxe-table-column field="" title="操作" width="110">
        <template #default="{ row,rowIndex }">
          <el-button @click="editEvent(row,rowIndex)" type="success" size="medium" icon="el-icon-edit" circle></el-button>
          <el-button @click="deleteEvent(row)" type="danger" size="medium" icon="el-icon-delete" circle></el-button>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager :loading="loading" :current-page="page.currentPage" :page-size="page.pageSize" :total="page.totalResult"
      :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']" @page-change="handlePageChange">
    </vxe-pager>
    <vxe-modal v-model="insertModal" title="比赛成绩" width="500" height="80%">
      <template #default>
        <el-form inline-message ref="insertForm" :rules="rules" size="medium" :model="insertForm" label-width="110px"  style="padding: 0 50px;">
          <el-form-item label="比赛日期" prop="matchDate">
            <el-date-picker type="date" placeholder="选择日期" v-model="insertForm.matchDate" style="width: 100%;">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="赛事名称" prop="matchName">
            <el-input v-model="insertForm.matchName"></el-input>
          </el-form-item>
          <el-form-item label="赛事级别" prop="matchLevelId">
            <el-select v-model="insertForm.matchLevelId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_matchLevel" :key="index">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运动项目" prop="sportId">
            <el-select v-model="insertForm.sportId" @change="sportChange" disabled filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_sport" :key="index">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运动小项" prop="sportItemId">
            <el-select v-model="insertForm.sportItemId" filterable clearable style="width: 100%;"
              placeholder="请选择">
              <el-option :label="item.name" :value="item.sportItemId" v-for="(item,index) in sportItemData" :key="index">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="赛事类别" prop="matchKindId">
            <el-select v-model="insertForm.matchKindId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_matchKin" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="参赛项目" prop="matchEventId">
            <el-select v-model="insertForm.matchEventId" @change="matchEventChange" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_matchEvent" :key="index">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="参赛小项" prop="matchEventItemId">
            <el-select v-model="insertForm.matchEventItemId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item.name" :value="item.matchEventItemId" v-for="(item,index) in matchEventItemData" :key="index">
              </el-option>
            </el-select>
            <el-link v-if="!matchEventItemIdShow" @click="matchEventItemIdInsertClick" style="font-size: 80%;" icon="el-icon-plus" type="success">点击新增</el-link>
            <el-input v-else style="margin-top: 10px;" placeholder="请输入参赛小项" v-model="matchEventItemName" class="input-with-select">
              <el-button @click="matchEventItemIdInsert" slot="append" icon="iconfont icon-save"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="教练" prop="coaches">
            <el-select v-model="insertForm.coaches" multiple filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_coach" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运动员" prop="playerId">
            <el-select v-model="insertForm.playerId" disabled filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_player" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别" prop="genderId">
            <el-select v-model="insertForm.genderId" disabled filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_gender" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="组别" prop="matchGroupTypeId">
            <el-select v-model="insertForm.matchGroupTypeId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_matchGroupType" :key="index"></el-option>
            </el-select>
            <el-link v-if="!matchGroupTypeIdShow" @click="matchGroupTypeIdShow = true" style="font-size: 80%;" icon="el-icon-plus" type="success">点击新增</el-link>
            <el-input v-else style="margin-top: 10px;" placeholder="请输入组别" v-model="matchGroupTypeName" class="input-with-select">
              <el-button @click="matchGroupTypeIdInsert" slot="append" icon="iconfont icon-save"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="比赛成绩" prop="matchResult">
            <el-input v-model="insertForm.matchResult"></el-input>
          </el-form-item>
          <el-form-item label="代表队" prop="delegationId">
            <el-select v-model="insertForm.delegationId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_delegation" :key="index"></el-option>
            </el-select>
            <el-link v-if="!delegationIdShow" @click="delegationIdShow = true" style="font-size: 80%;" icon="el-icon-plus" type="success">点击新增</el-link>
            <el-input v-else style="margin-top: 10px;" placeholder="请输入代表队" v-model="delegationName" class="input-with-select">
              <el-button @click="delegationIdInsert" slot="append" icon="iconfont icon-save"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="名次" prop="matchRank">
            <el-input v-model.number="insertForm.matchRank" type="number" @input="changeInput" @focus="changeInput"></el-input>
          </el-form-item>
          <el-form-item label="破纪录" prop="recordBreakId">
            <el-select v-model="insertForm.recordBreakId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_recordBreak" :key="index">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="个人/团体" prop="multiPlayerMatch">
            <el-radio v-model="insertForm.multiPlayerMatch" label="个人"></el-radio>
            <el-radio v-model="insertForm.multiPlayerMatch" label="团体"></el-radio>
          </el-form-item>
          <el-form-item label="协议" prop="protocol">
            <el-input v-model="insertForm.protocol"></el-input>
          </el-form-item>
          <el-form-item label="比赛地点" prop="matchAddressId">
            <el-select v-model="insertForm.matchAddressId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_matchAddress" :key="index">
              </el-option>
            </el-select>
            <el-link v-if="!matchAddressIdShow" @click="matchAddressIdShow = true" style="font-size: 80%;" icon="el-icon-plus" type="success">点击新增</el-link>
            <el-input v-else style="margin-top: 10px;" placeholder="请输入比赛地点" v-model="matchAddressName" class="input-with-select">
              <el-button @click="matchAddressIdInsert" slot="append" icon="iconfont icon-save"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="备注" prop="memo">
            <el-input v-model="insertForm.memo"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="warning" size="small" @click="insertModal = false">取消</el-button>
            <el-button type="success" size="small" icon="el-icon-s-promotion" @click="insertSave">保存</el-button>
          </el-form-item>
        </el-form>
      </template>
    </vxe-modal>
  </div>
</template>
<script>
  import {
    person_match,
    sport_item_by_sportId,
    person_match_insert,
    person_match_update,
    person_match_delete,
    getPlayerRoughly,
    match_event_item_by_matchEventId,
    match_event_item_insert,
    match_group_type_insert,
    delegation_insert,
    match_address_insert
  } from '@/api/person.js'
  import {
    coachByOrgIgnoreRole,
    currentRolePlayer,
    sport_by_org,
    sport_item,
    match_event,
    match_event_item,
    getDelegation,
    record_break,
    match_level,
    match_group_type,
    match_kind,
    match_address
  } from '@/api/dict.js'
  export default {
    props: ['pId'],
    data() {
      return {
        defaultDataList: [],
        dataList: [],
        page: {
          currentPage: 1,
          pageSize: 10,
          totalResult: 0
        },
        loading: false,
        filterName: '',
        insertModal: false,
        dict_gender: {1:'男',2:'女'},
        dict_coach: {},
        dict_matchLevel: {},
        dict_matchKin: {},
        dict_matchEvent: {},
        dict_matchEventItem: {},
        dict_player: {},
        dict_delegation: {},
        dict_recordBreak: {},
        dict_multiPlayerMatch: {'个人':'个人','团体':'团体'},
        dict_matchAddress: {},
        dict_matchGroupType: {},
        insertForm: {
          coaches: '',
          matchDate: '',
          matchName: '',
          matchLevelId: '',
          sportId: '',
          sportItemId: '',
          matchKindId: '',
          matchEventId: '',
          matchEventItemId: '',
          playerId: '',
          genderId: '',
          matchGroupTypeId: '',
          matchResult: '',
          delegationId: '',
          matchRank: '',
          recordBreakId: '',
          multiPlayerMatch: '',
          protocol: '',
          matchAddressId: '',
          memo: '',
        },
        sportItemData: [],
        matchEventItemData: [],
        rules: {
          matchDate: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
          matchName: [{
            required: true,
            message: '这是必填项',
            trigger: 'blur'
          }, ],
          sportId: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
          matchEventId: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
          matchEventItemId: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
          multiPlayerMatch: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
        },
        isInsert: true,
        matchId: '',
        matchEventItemIdShow: false,
        matchEventItemName: '',
        matchGroupTypeIdShow: false,
        matchGroupTypeName: '',
        delegationIdShow: false,
        delegationName: '',
        matchAddressIdShow: false,
        matchAddressName: '',
        columns: [],
        search: false,
        currentIndex: null,
      }
    },
    created() {
      this.init_dict().then(()=>{
        this.person_match()
      })
    },
    watch: {
      filterName(val) {
    	if(!val){
    	  this.page = {
    	  currentPage: 1,
    	  pageSize: 10,
    	  totalResult: 0,
    	  }
    	  this.search = false
    	  this.findList()
    	}
      }
    },
    mounted() {
      this.$nextTick(() => {
        let arr = this.$refs.XTable.getColumns()
        // let visibleArr = ['delegation','regPR','regCT','reg']
        let visibleArr = ['delegation']
        arr.forEach(item=>{
          if(visibleArr.indexOf(item.property) > -1 ){
            item.visible = false
            this.columns.push(item)
          }
          if (this.$refs.XTable) {
            this.$refs.XTable.refreshColumn()
          }
        })
      })
    },
    computed: {
      openDateValid() {
        if (this.insertForm.openDate && this.insertForm.closeDate) {
          if (this.insertForm.openDate > this.insertForm.closeDate) {
            this.$message.error('开始时间必须小于结束时间！')
            return false
          } else {
            return true
          }
        } else {
          return true
        }
      }
    },
    methods: {
      changeInput() {
        var pattern = /^[1-9][0-9]*$/ // 正整数的正则表达式
        // 不符合正整数时
        if (!pattern.test(this.insertForm.matchRank)) {
          // input 框绑定的内容为空
          this.insertForm.matchRank = ''
        }
      },
      matchAddressIdInsert(){
        match_address_insert({name:this.matchAddressName}).then(res=>{
          if(res.code === 200){
            this.$message.success('添加成功！')
            match_address().then(res => {
              this.dict_matchAddress = res
            })
            this.matchAddressIdShow = false
          }else{
            this.$message.error('添加失败！')
            this.matchAddressIdShow = false
          }
        })
      },
      delegationIdInsert(){
        delegation_insert({name:this.delegationName}).then(res=>{
          if(res.code === 200){
            this.$message.success('添加成功！')
            getDelegation().then(res => {
              this.dict_delegation = res
            })
            this.delegationIdShow = false
          }else{
            this.$message.error('添加失败！')
            this.delegationIdShow = false
          }
        })
      },
      matchGroupTypeIdInsert(){
        match_group_type_insert({name:this.matchGroupTypeName}).then(res=>{
          if(res.code === 200){
            this.$message.success('添加成功！')
            match_group_type().then(res => {
              this.dict_matchGroupType = res
            })
            this.matchGroupTypeIdShow = false
          }else{
            this.$message.error('添加失败！')
            this.matchGroupTypeIdShow = false
          }
        })
      },
      matchEventItemIdInsert(){
        match_event_item_insert({name:this.matchEventItemName,matchEventId:this.insertForm.matchEventId}).then(res=>{
          if(res.code === 200){
            this.$message.success('添加成功！')
            this.matchEventItemData.unshift(res.data)
            this.matchEventItemIdShow = false
          }else{
            this.$message.error('添加失败！')
            this.matchEventItemIdShow = false
          }
        })
      },
      matchEventItemIdInsertClick(){
        if(this.insertForm.matchEventId){
          this.matchEventItemIdShow = true
        }else{
          this.$message.warning('请选择所属的参赛项目')
        }
      },
      deleteEvent(row) {
        this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          person_match_delete({
            matchId: row.matchId
          }).then(res => {
            if (res.code === 200) {
              this.defaultDataList.forEach((item,index)=>{
              	if(item.matchId == row.matchId){
              	  this.defaultDataList.splice(index,1)
              	}
              })
              this.init_data(this.defaultDataList)
              this.findList()
              this.$message.success('删除成功！')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      editEvent(row,rowIndex) {
        this.currentIndex = rowIndex
        this.insertForm = {
          coaches: '',
          matchDate: '',
          matchName: '',
          matchLevelId: '',
          sportId: '',
          sportItemId: '',
          matchKindId: '',
          matchEventId: '',
          matchEventItemId: '',
          playerId: '',
          genderId: '',
          matchGroupTypeId: '',
          matchResult: '',
          delegationId: '',
          matchRank: '',
          recordBreakId: '',
          multiPlayerMatch: '',
          protocol: '',
          matchAddressId: '',
          memo: '',
        }
        this.matchId = row.matchId
        if (row.coaches) {
          this.insertForm.coaches = row.coaches.split(',')
        }else{
          this.insertForm.coaches = ''
        }
        if (row.matchDate) this.insertForm.matchDate = this.$moment(row.matchDate).format('YYYY-MM-DD') + ' 00:00:00'
        if (row.matchName) this.insertForm.matchName = row.matchName
        if (row.matchLevelId) this.insertForm.matchLevelId = row.matchLevelId+''
        if (row.sportId) this.insertForm.sportId = row.sportId+''
        this.sportChange()
        if (row.sportItemId) this.insertForm.sportItemId = row.sportItemId
        if (row.matchKindId) this.insertForm.matchKindId = row.matchKindId+''
        if (row.matchEventId) this.insertForm.matchEventId = row.matchEventId+''
        this.matchEventChange()
        if (row.matchEventItemId) this.insertForm.matchEventItemId = row.matchEventItemId
        if (row.playerId) this.insertForm.playerId = row.playerId+''
        if (row.genderId) this.insertForm.genderId = row.genderId+''
        if (row.matchGroupTypeId) this.insertForm.matchGroupTypeId = row.matchGroupTypeId+''
        if (row.matchResult) this.insertForm.matchResult = row.matchResult
        if (row.delegationId) this.insertForm.delegationId = row.delegationId+''
        if (row.matchRank) this.insertForm.matchRank = row.matchRank
        if (row.recordBreakId) this.insertForm.recordBreakId = row.recordBreakId+''
        if (row.multiPlayerMatch) this.insertForm.multiPlayerMatch = row.multiPlayerMatch
        if (row.protocol) this.insertForm.protocol = row.protocol
        if (row.matchAddressId) this.insertForm.matchAddressId = row.matchAddressId+''
        if (row.memo) this.insertForm.memo = row.memo
        this.isInsert = false
        this.insertModal = true
      },
      insertEvent() {
        this.insertForm = {
          coaches: '',
          matchDate: '',
          matchName: '',
          matchLevelId: '',
          sportId: '',
          sportItemId: '',
          matchKindId: '',
          matchEventId: '',
          matchEventItemId: '',
          playerId: '',
          genderId: '',
          matchGroupTypeId: '',
          matchResult: '',
          delegationId: '',
          matchRank: '',
          recordBreakId: '',
          multiPlayerMatch: '',
          protocol: '',
          matchAddressId: '',
          memo: '',
        }
        this.sportChange()
        getPlayerRoughly(this.pId).then(res=>{
          if(res.code === 200) {
            this.insertForm.sportId = res.data.sportId==0?'': res.data.sportId+''
            this.insertForm.genderId = res.data.genderId==0?'': res.data.genderId+''
          }
        })
        this.isInsert = true
        this.insertModal = true
      },
      insertSave() {
        this.$refs.insertForm.validate((valid) => {
          if (valid && this.openDateValid) {
            let data = {}
            if (this.insertForm.coaches) data.coaches = this.insertForm.coaches.join(',')
            if (this.insertForm.matchDate) data.matchDate = this.$moment(this.insertForm.matchDate).format(
              'YYYY-MM-DD') + ' 00:00:00'
            if (this.insertForm.matchName) data.matchName = this.insertForm.matchName
            if (this.insertForm.matchLevelId) data.matchLevelId = this.insertForm.matchLevelId
            if (this.insertForm.sportId) data.sportId = this.insertForm.sportId
            if (this.insertForm.sportItemId) data.sportItemId = this.insertForm.sportItemId
            if (this.insertForm.matchKindId) data.matchKindId = this.insertForm.matchKindId
            if (this.insertForm.matchEventId) data.matchEventId = this.insertForm.matchEventId
            if (this.insertForm.matchEventItemId) data.matchEventItemId = this.insertForm.matchEventItemId
            if (this.insertForm.playerId) data.playerId = this.insertForm.playerId
            if (this.insertForm.genderId) data.genderId = this.insertForm.genderId
            if (this.insertForm.matchGroupTypeId) data.matchGroupTypeId = this.insertForm.matchGroupTypeId
            if (this.insertForm.matchResult) data.matchResult = this.insertForm.matchResult
            if (this.insertForm.delegationId) data.delegationId = this.insertForm.delegationId
            if (this.insertForm.matchRank) data.matchRank = this.insertForm.matchRank
            if (this.insertForm.recordBreakId) data.recordBreakId = this.insertForm.recordBreakId
            if (this.insertForm.multiPlayerMatch) data.multiPlayerMatch = this.insertForm.multiPlayerMatch
            if (this.insertForm.protocol) data.protocol = this.insertForm.protocol
            if (this.insertForm.matchAddressId) data.matchAddressId = this.insertForm.matchAddressId
            if (this.insertForm.memo) data.memo = this.insertForm.memo
            if (this.insertForm.memo) data.matchTypeId = 0
            if (this.isInsert) {
              person_match_insert({
                pId: this.pId,
                data: data
              }).then(res => {
                if (res.code === 200) {
                  this.dataList.unshift(res.data)
                  this.defaultDataList.push(res.data)
                  this.init_data(this.defaultDataList)
                  this.page.totalResult+=1
                  this.$refs.XTable.setCurrentRow(this.dataList[0])
                  this.$message.success('保存成功')
                  this.insertSuccess = true
                  this.insertModal = false
                } else {
                  this.$message.error(res.message)
                }
              })
            } else {
              data.matchId = this.matchId
              person_match_update(data).then(res => {
                if (res.code === 200) {
                  Object.assign(this.dataList[this.currentIndex],res.data)
                  this.defaultDataList.forEach(item=>{
                  	if(res.data.matchId == item.matchId){
                  	  Object.assign(item,res.data)
                  	}
                  })
                  this.init_data(this.defaultDataList)
                  this.$message.success('修改成功')
                  this.insertModal = false
                } else {
                  this.$message.error(res.message)
                }
              })
            }
          } else {
            return false;
          }
        })
      },
      sportChange() {
        this.insertForm.sportItemId = ''
        sport_item_by_sportId({
          sportId: this.insertForm.sportId
        }).then(res => {
          if (res.code === 200) {
            this.sportItemData = res.data
          }
        })
      },
      matchEventChange() {
        this.insertForm.matchEventItemId = ''
        match_event_item_by_matchEventId(this.insertForm.matchEventId).then(res => {
          if (res.code === 200) {
            this.matchEventItemData = res.data
          }
        })
      },
      printEvent() {
        let topHtml = '<h2>比赛成绩</h2>'
        this.searchEvent()
        setTimeout(() => {
          this.$refs.XTable.print({
            sheetName: '比赛成绩',
            beforePrintMethod: ({
              content
            }) => {
              return topHtml + content
            }
          })
        }, 1000)
      },
      searchEvent() {
        const filterName = this.$XEUtils.toValueString(this.filterName).trim().toLowerCase()
        if (filterName) {
          const filterRE = filterName
          const searchProps = this.searchProps
          const rest = this.defaultDataList.filter(item => searchProps.some(key => this.$XEUtils.toValueString(item[
            key]).toLowerCase().indexOf(filterName) > -1))
          this.dataList = rest.map(row => {
            const item = Object.assign({}, row)
            searchProps.forEach(key => {
              item[key] = this.$XEUtils.toValueString(item[key]).replace(filterRE, match => match)
            })
            return item
          })
          this.page = {
          	currentPage: 1,
          	pageSize: this.dataList.length,
          	totalResult: this.dataList.length
            }
            this.search = true
        } else {
          this.dataList = this.defaultDataList
        }
      },
      person_match() {
          person_match(this.pId).then(res => {
            if (res.code === 200 && res.data.length != 0) {
              let data = res.data
              this.init_data(data)
              this.$emit('matchResultDataPrint',this.defaultDataList)
            } else {
              this.defaultDataList = []
            }
          }).then(() => {
            this.findList()
          })
      },
      init_data(data) {
        if(data.length==0) return
        data.forEach(item => {
          if(item.coaches){
            let arr = item.coaches.split(',')
            let coachArr = []
            arr.forEach(coach=>{
              coachArr.push(this.dict_coach[coach])
            })
            item.coacheNames = coachArr.join(',')
          }
          item.matchDate = this.$moment(item.matchDate).format('YYYY-MM-DD') == 'Invalid date' ? '' :
            this.$moment(item.matchDate).format('YYYY-MM-DD')
          item.matchLevel = this.dict_matchLevel[item.matchLevelId]
          item.matchKin = this.dict_matchKin[item.matchKindId]
          item.matchEvent = this.dict_matchEvent[item.matchEventId]
          item.matchEventItem = this.dict_matchEventItem[item.matchEventItemId]
          item.delegation = this.dict_delegation[item.delegationId]
          item.recordBreak = this.dict_recordBreak[item.recordBreakId]
          item.matchRank = item.matchRank==0?'':item.matchRank
        })
        this.searchProps = Object.keys(data[0])
        this.defaultDataList = data
      },
      init_dict() {
        return new Promise((resolve, reject) => {
          coachByOrgIgnoreRole().then(res => {
            this.dict_coach = res
          }).then(() => {
            sport_by_org().then(res => {
              this.dict_sport = res
            })
          }).then(() => {
            sport_item().then(res => {
              this.dict_sportItem = res
            })
          }).then(() => {
            match_event().then(res => {
              this.dict_matchEvent = res
            })
          }).then(() => {
            match_event_item().then(res => {
              this.dict_matchEventItem = res
            })
          }).then(() => {
            getDelegation().then(res => {
              this.dict_delegation = res
            })
          }).then(() => {
            record_break().then(res => {
              this.dict_recordBreak = res
            })
          }).then(() => {
            match_level().then(res => {
              this.dict_matchLevel = res
            })
          }).then(() => {
            match_group_type().then(res => {
              this.dict_matchGroupType = res
            })
          }).then(() => {
            match_kind().then(res => {
              this.dict_matchKin = res
            })
          }).then(() => {
            currentRolePlayer().then(res => {
              this.dict_player = res
            })
          }).then(() => {
            match_address().then(res => {
              this.dict_matchAddress = res
              resolve()
            })
          })
        })
      },
      handlePageChange({
        currentPage,
        pageSize
      }) {
        this.page.currentPage = currentPage
        this.page.pageSize = pageSize
        this.findList()
      },
      sortChange() {
      	if(!this.search){
          this.page.currentPage = 1
      	  this.page.pageSize = this.defaultDataList.length
      	  this.findList()
      	}else{
      	  this.page.pageSize = this.dataList.length
      	}
        },
      findList() {
        this.loading = true
        setTimeout(() => {
          if(this.insertSuccess){
            this.page.totalResult = this.defaultDataList.length
            this.page.currentPage = Math.ceil(this.page.totalResult/this.page.pageSize)
            this.dataList = this.defaultDataList.slice((this.page.currentPage - 1) * this.page
              .pageSize, this.page.currentPage * this.page.pageSize)
            this.$refs.XTable.setCurrentRow(this.dataList[this.dataList.length-1])
            this.insertSuccess = false
          }else{
            this.page.totalResult = this.defaultDataList.length
            this.dataList = this.defaultDataList.slice((this.page.currentPage - 1) * this.page
              .pageSize, this.page.currentPage * this.page.pageSize)
          }
          this.loading = false
        }, 300)
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
